package com.team06.util;

import com.team06.pojo.Blls;
import com.team06.pojo.BllsBelt;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class BllsXmlParserUtil {
    private static final Logger LOGGER = Logger.getLogger(BllsXmlParserUtil.class.getName());

    public static Blls parseXml(String xmlPath) {
        Blls entity = new Blls();
        try {
            File file = new File(xmlPath);
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(file);
            Element root = document.getRootElement();

            // 解析 META 节点
            Element metaElement = root.getChild("META");
            if (metaElement != null) {
                entity.setSender(metaElement.getChildText("SNDR"));
                entity.setReceiver(metaElement.getChildText("RCVR"));
                entity.setSequence(Integer.parseInt(metaElement.getChildText("SEQN")));
                entity.setDtime(metaElement.getChildText("DDTM"));
                entity.setMtype(metaElement.getChildText("TYPE"));
                entity.setStype(metaElement.getChildText("STYP"));
            }

            // 解析 DFLT 节点
            Element dfltElement = root.getChild("DFLT");
            if (dfltElement != null) {
                entity.setDfltFlid(dfltElement.getChildText("FLID"));
                entity.setDfltFfid(dfltElement.getChildText("FFID"));
                entity.setDfltFide(dfltElement.getChildText("FIDE"));
                entity.setDfltFatt(dfltElement.getChildText("FATT"));

                // 解析 BLLS 节点下的 BELT 节点
                Element bllsElement = dfltElement.getChild("BLLS");
                if (bllsElement != null) {
                    Element beltElement = bllsElement.getChild("BELT");
                    if (beltElement != null) {
                        entity.setBllsBeltId(beltElement.getChildText("ID"));
                    }
                }
            }
        } catch (JDOMException | IOException | NumberFormatException e) {
            LOGGER.log(Level.SEVERE, "Failed to parse BLLS XML file: " + xmlPath, e);
            return null;
        }
        return entity;
    }

    public static List<BllsBelt> parseBllsBelts(String xmlPath) {
        List<BllsBelt> belts = new ArrayList<>();
        try {
            File file = new File(xmlPath);
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(file);
            Element root = document.getRootElement();

            // 解析 DFLT 节点获取航班ID
            Element dfltElement = root.getChild("DFLT");
            if (dfltElement != null) {
                String flightId = dfltElement.getChildText("FLID");

                // 解析 BLLS 节点下的 BELT 节点
                Element bllsElement = dfltElement.getChild("BLLS");
                if (bllsElement != null) {
                    List<Element> beltElements = bllsElement.getChildren("BELT");
                    for (Element beltElement : beltElements) {
                        BllsBelt belt = new BllsBelt();
                        belt.setBtno(beltElement.getChildText("BTNO"));
                        belt.setBeltId(beltElement.getChildText("ID"));
                        belt.setCode(beltElement.getChildText("CODE"));
                        belt.setBtat(beltElement.getChildText("BTAT"));
                        belt.setEstr(beltElement.getChildText("ESTR"));
                        belt.setEend(beltElement.getChildText("EEND"));
                        belt.setRstr(beltElement.getChildText("RSTR"));
                        belt.setRend(beltElement.getChildText("REND"));
                        belt.setBtsc(beltElement.getChildText("BTSC"));
                        belts.add(belt);
                    }
                }
            }
        } catch (JDOMException | IOException e) {
            LOGGER.log(Level.SEVERE, "Failed to parse BLLS BELT XML file: " + xmlPath, e);
        }
        return belts;
    }
}